package com.zx.wangyixinwen.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.zx.wangyixinwen.R;
import com.zx.wangyixinwen.activity.NewsDetailsActivity;
import com.zx.wangyixinwen.activity.TopicActivity;
import com.zx.wangyixinwen.adapter.TopicAdapter;
import com.zx.wangyixinwen.bean.Topic;
import com.zx.wangyixinwen.bean.TopicBean;
import com.zx.wangyixinwen.refresh.SwipeRefreshLoadingLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/4.
 */
public class TopicFragment extends Fragment implements SwipeRefreshLoadingLayout.OnRefreshListener{
    private Toolbar toolbar;
    private ListView listView;
    private RequestQueue queue;
    private List<Topic> datas;
    private TopicAdapter adapter;
    private SwipeRefreshLoadingLayout swipeRefreshLoadingLayout;
    private Context context;
    private String url = "http://c.3g.163.com/newstopic/list/expert/0-10.html";

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_topic, null);
        setHasOptionsMenu(true);
        toolbar = (Toolbar) view.findViewById(R.id.toolbar_tppic);
        toolbar.setTitle("问吧");
        toolbar.setTitleTextColor(Color.WHITE);
        listView = (ListView) view.findViewById(R.id.listView_topic);
        swipeRefreshLoadingLayout = (SwipeRefreshLoadingLayout) view.findViewById(R.id.topic_refresh);
        swipeRefreshLoadingLayout.setOnRefreshListener(this);
        addData();

        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.topic_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    private void addData() {
        queue = Volley.newRequestQueue(getActivity());
        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                datas = new ArrayList<>();
                final Topic topic = gson.fromJson(response, Topic.class);
                datas.add(topic);
                for (int i = 0; i < topic.getData().getExpertList().size(); i++) {
                    TopicBean topicDetals = new TopicBean();
                    topicDetals.setPicurl(datas.get(0).getData().getExpertList().get(i).getPicurl());
                    topicDetals.setHeadpicurl(datas.get(0).getData().getExpertList().get(i).getHeadpicurl());
                    topicDetals.setName(datas.get(0).getData().getExpertList().get(i).getName());
                    topicDetals.setDescription(datas.get(0).getData().getExpertList().get(i).getAlias());
                }

                adapter = new TopicAdapter(context, datas);
                listView.setAdapter(adapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
                        TopicActivity.goToTopic(context,
                                datas.get(0).getData().getExpertList().get(position));
                    }
                });

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(stringRequest);
    }

    @Override
    public void onRefresh() {
        addData();
        swipeRefreshLoadingLayout.setRefreshing(false);
    }
}
